﻿using System;
using System.Web.UI.WebControls;
using TrafficArchives.Common;
using TrafficArchives.DataAccess;

namespace TrafficArchives.Logs
{
    public partial class LogView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initilize();

                QueryData();
            }
        }

        private void Initilize()
        {
            TxtStartDate.Attributes.Add("onclick", "WdatePicker()");
            TxtEndDate.Attributes.Add("onclick", "WdatePicker()");
        }

        private void QueryData()
        {
            var request = new Request {Pagination = {Enabled = true, PageSize = MainPager.PageSize, PageIndex = MainPager.CurrentPageIndex -1}};
            if (!string.IsNullOrEmpty(TxtDriverNo.Text))
            {
                var con = new Condition
                              {
                                  FieldName = "BCZRJSZH",
                                  Operator = ConditionOperator.Like,
                                  Values = new object[] { TxtDriverNo.Text }
                              };
                request.Items.Add(con);
            }
            if (!string.IsNullOrEmpty(TxtArchivesNo.Text))
            {
                var con = new Condition
                {
                    FieldName = "BCZRDABH",
                    Operator = ConditionOperator.Like,
                    Values = new object[] { TxtArchivesNo.Text }
                };
                request.Items.Add(con);
            }
            if (!string.IsNullOrEmpty(DdlLogType.SelectedValue))
            {
                var con = new Condition
                {
                    FieldName = "CZTYPENAME",
                    Operator = ConditionOperator.Equal,
                    Values = new object[] { DdlLogType.SelectedValue }
                };
                request.Items.Add(con);
            }

            if (!string.IsNullOrEmpty(TxtStartDate.Text))
            {
                var con = new Condition
                {
                    FieldName = "to_char(t.CZTIME, 'yyyy-mm-dd')",
                    Operator = ConditionOperator.GreaterAndEqual,
                    Values = new object[] { TxtStartDate.Text }
                };
                request.Items.Add(con);
            }

            if (!string.IsNullOrEmpty(TxtEndDate.Text))
            {
                var con = new Condition
                {
                    FieldName = "to_char(t.CZTIME, 'yyyy-mm-dd')",
                    Operator = ConditionOperator.LessAndEqual,
                    Values = new object[] { TxtEndDate.Text }
                };
                request.Items.Add(con);
            }
            
            var db = new LogBb();
            var data = db.GetData(request);
            MainPager.RecordCount = data.RecordCount;
            GridViewMain.DataSource = data.Data;
            GridViewMain.DataBind();

            // 没数据时，显示表头。
            TableHeader.Visible = data.RecordCount == 0;
            UpdatePanelMain.Update();
        }

        protected void MainPagerPageIndexChanged(object sender, EventArgs e)
        {
            QueryData();
        }

        protected void BtnQueryClicked(object sender, EventArgs e)
        {
            MainPager.CurrentPageIndex = 1;

            QueryData();
        }

        protected void BtnAddClicked(object sender, EventArgs e)
        {
        }
    }
}